import { useState } from 'react'
import { IMcc } from '@/api/manager/type'
import { Mt } from '@/api/manager/mt'
import { SimpleUserModel, UserModel } from '../../blocks/user/interface'
import { useBasicInfoStore } from '../../blocks/basicInfo/store'
import { isMatch } from '@/pages/manager/utils/utils'
import { UserEntity } from '@/pages/index/ManageList/interfaces/user'

export interface ItemData {
	mcc: IMcc
	nestingLevel: number
	mtKey: string
}
export interface SelectedModel {
	mcc: UserEntity
	status: {
		checked: boolean
	}
	uuid: string
}
interface IL {
	mcc: Mt
	mtKey: string
}

export default function useGetList() {
	const [list, setList] = useState<UserModel[]>([])
	const [openMap, setOpenMap] = useState<{ [mtKey: string]: boolean }>({})
	const flatAllTreeToListNoOpenMap = (tree: UserModel[]) => {
		let stack = [...tree]
		let result = []
		while (stack.length !== 0) {
			const item = stack.shift()!
			result.push({
				mcc: item.mcc,
				status: item.status,
				attr: item.attr,
				uuid: item.uuid,
				parentId: item.parentId,
				nestingLevel: item.nestingLevel,
				children: [],
			})
			if (item.mcc.account_type === 10 && !item.attr.isSimpleCascade) {
				for (let i = item.children.length - 1; i >= 0; i--) {
					let o = item.children[i]
					stack.unshift({
						mcc: o.mcc,
						status: o.status,
						attr: o.attr,
						uuid: o.uuid,
						parentId: o.parentId,
						children: o.children,
						nestingLevel: item.nestingLevel + 1,
					})
				}
			}
		}
		return result
	}
	const flatAllTreeToList = (tree: UserModel[], actualSearchVal: string) => {
		let stack = [...tree]
		let result = []
		while (stack.length !== 0) {
			const item = stack.shift()!
			const superMtNum = useBasicInfoStore.getState().superMtNum
			/**此处需要过滤会议终端*/
			if (!superMtNum && item.mcc.account_type === 10 && !item.attr.isSimpleCascade) {
				/**非超大方非级联会议不需要参与筛选*/
				result.push({
					mcc: item.mcc,
					status: item.status,
					attr: item.attr,
					uuid: item.uuid,
					parentId: item.parentId,
					nestingLevel: item.nestingLevel,
					children: [],
				})
			} else {
				let isMatchSearch = isMatch(item.mcc, actualSearchVal)
				if (isMatchSearch) {
					result.push({
						mcc: item.mcc,
						status: item.status,
						attr: item.attr,
						uuid: item.uuid,
						parentId: item.parentId,
						nestingLevel: item.nestingLevel,
						children: [],
					})
				}
			}
			/**非超大方非级联会议不需要参与筛选  判断是否需要展开  如果需要就展开否则不需要遍历*/
			if (item.mcc.account_type === 10 && !item.attr.isSimpleCascade && openMap[item.uuid]) {
				for (let i = item.children.length - 1; i >= 0; i--) {
					let o = item.children[i]
					stack.unshift({
						mcc: o.mcc,
						status: o.status,
						attr: o.attr,
						uuid: o.uuid,
						parentId: o.parentId,
						children: o.children,
						nestingLevel: item.nestingLevel + 1,
					})
				}
			}
		}
		console.log(result)
		return result
	}
	/**获取全部终端 */

	const getListFromTableAll = (tree: UserModel[], actualSearchVal: string) => {
		setList(flatAllTreeToList(tree, actualSearchVal))
	}
	/**获取常用终端*/
	const getListFromTableOften = (tree: SimpleUserModel[], keywords: string) => {
		let result = []
		for (let i = 0; i < tree.length; i++) {
			const item = tree[i]
			let isMatchSearch = isMatch(item.mcc, keywords)
			if (isMatchSearch) {
				result.push({
					mcc: item.mcc,
					nestingLevel: 0,
					children: [],
					uuid: item.uuid,
					parentId: item.parentId,
					status: {
						checked: false,
						isOpen: false,
						isMatchSearch: false,
						isOften: false,
						isInteractive: false,
					},
					attr: {
						actualUuid: item.uuid,
						isSimpleCascade: false,
					},
				})
			}
		}
		setList(result)
	}
	/**获取互动终端*/
	const getListFromTableInteractive = (tree: SimpleUserModel[], keywords: string) => {
		let result = []
		for (let i = 0; i < tree.length; i++) {
			const item = tree[i]
			let isMatchSearch = isMatch(item.mcc, keywords)
			if (isMatchSearch) {
				result.push({
					mcc: item.mcc,
					nestingLevel: 0,
					children: [],
					uuid: item.uuid,
					parentId: item.parentId,
					status: {
						checked: false,
						isOpen: false,
						isMatchSearch: false,
						isOften: false,
						isInteractive: false,
					},
					attr: {
						actualUuid: item.uuid,
						isSimpleCascade: false,
					},
				})
			}
		}
		setList(result)
	}

	return {
		list,
		openMap,
		setOpenMap,
		getListFromTableAll,
		getListFromTableOften,
		getListFromTableInteractive,
		flatAllTreeToList,
		setList,
	}
}
